package cn.cdl.report.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.cdl.report.entity.ChinaBankWuxiXinqu;
import cn.cdl.report.service.ChinaBankWuxiXinquService;
import cn.cdl.report.util.ExcelUtil;
import cn.cdl.report.util.FileUtil;

/**
 * @author cdl
 * 2018年3月25日 下午2:51:59
 */
@Controller
public class Index{
	
	@Autowired
	ChinaBankWuxiXinquService chinaBankWuxiXinquServiceImpl;
	
	@RequestMapping("/index")
	public String index(){
		//chinaBankWuxiXinquServiceImpl.add(new ChinaBankWuxiXinqu());
		return "index";
	}

	@RequestMapping("/toUploadFile")
	public String toUpload(){
		
		return "uploadfile";
	}
	
	@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
	@ResponseBody
	public void upload(@RequestParam("file") MultipartFile file ,HttpServletRequest request){
		System.out.println("========="+file.getOriginalFilename()+"======"+file.getContentType());
		try {
			FileUtil.uploadFile(file.getBytes(), "d:\\", "test.xlsx");
			//解析excel
			InputStream input = new ByteArrayInputStream(file.getBytes());
			ExcelUtil util = new ExcelUtil(file);
			List<String[]> list = util.readSheetByName("中国银行无锡支行");
			
			System.out.println(list.size()+"---");
			for(String[] as:list){
				for(String str:as){
					System.out.print(str+"\t");
				}
				System.out.println("\n");
			}
			chinaBankWuxiXinquServiceImpl.batchInsert(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
}